<?php
function form_print(){
 $view = form_header( "dat" , "dat"  );
	 
	$view .= '<input type="hidden"  value="'.$_GET['com'].'" name="com" />';  
	$form_start = '<input type="text" size="7" value="'.$nik_start.'" name="nik_start" />';
	$form_end = '<input type="text" size="7" value="'.$nik_end.'" name="nik_end" />';
	$view .= form_field_display( $form_start.' s/d '.$form_end  , "NIK Range"   );
	$submit = array(
		'value' => (  ' Check '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 	
	$cetak = array(
		'value' => (  ' Cetak '  ),
		'onclick'=> (  
					'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=print&nik_start=\'+document.dat.nik_start.value+\'&nik_end=\'+document.dat.nik_end.value '
					  ),
		
		'type'=>'button','class'=>'main_button'
	);
	$form_cetak= form_dynamic($cetak); 	
 
	$excel = array(
		'value' => (  ' Broadcast absen '  ),
		'onclick' => 'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=sendmail&nik_start=\'+document.dat.nik_start.value+\'&nik_end=\'+document.dat.nik_end.value  });', 
		'type'=>'button',
		'style'=>'width:120px',
		'class'=>'main_button'
	);
	$form_excel	= form_dynamic($excel);
 
	$view .= form_field_display(   $form_cetak .' '.$form_excel, "&nbsp;" ,  "" );
	$view .= form_footer( );
	$view = str_replace("method=\"post\"","method=\"get\"",$view);
	return $view;

}
	
 
function get_html_content($nik){ 
	$content = URLopen ( 'http://'.$_SERVER['HTTP_HOST'].'/slip_absen.php?nik_start='.$nik.'&nik_end='.$nik);
  
	return $content;
}

function URLopen($url) 
{ 
        // Fake the browser type 
        ini_set('user_agent','MSIE 4\.0b2;'); 

        $dh = fopen("$url",'r'); 
        $result = fread($dh,8192);                                                                                                                             
        return $result; 
} 

function mailing_slip_absen($periode_absen , $nama , $nik , $email_target, $content){
	$mail             = new PHPMailer(); 
	  
	$mail->IsSMTP(); // telling the class to use SMTP 
	$mail->SMTPDebug  = 0;                      
	$mail->SMTPSecure = 'tls';                 
	$mail->Host       = "localhost"; 
	$mail->Port       = 25; 
	$mail->Username = "admin"; // SMTP username 
	$mail->Password = "dordordor"; // SMTP password   

	$mail->From = 'admin@hr-kmh.com';
	$mail->FromName = 'SDM PT. KRAKATAU MEDIKA';
	$mail->AddReplyTo("admin@hr-kmh.com","No-Reply"); 

	$mail->AddAddress($email_target , $nama );  
	if( $email_target == '1000290@hr-kmh.com' ){
		$mail->AddBCC('gunturris@gmail.com', 'Guntur');
	}
  
	$mail->Subject   	= "Slip absensi [{$nik} {$periode_absen}]";
	$mail->IsHTML(true);
	
	$mail->Body     	= $content;
	$test = $mail->send(); 
 
return $test;
}  
 
function send_email(){ 
	require_once('components/phpmailer/class.phpmailer.php'); 
	$periode_absen_id=get_periode_absen() ;
	$dperiode = my_get_data_by_id('pr_periode','periode_id',$periode_absen_id);
	$bulans = array(
		1=>'Januari',
		2=>'Februari',
		3=>'Maret',
		4=>'April',
		5=>'Mei',
		6=>'Juni',
		7=>'Juli',
		8=>'Agustus',
		9=>'September',
		10=>'Oktober',
		11=>'November',
		12=>'Desember',
	);
	$bln = (int)date('m' ,strtotime($dperiode['tanggal']));
	$namabulan = $bulans[$bln]; 
	$thn = date('Y' ,strtotime($dperiode['tanggal']));	
	$periode_absen = $namabulan .' '.$thn; 

	$allow = array('1000010','1000290','0000116','1000028','1000116');

	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 
				AND ( a.nik between '{$_GET['nik_start']}' AND '{$_GET['nik_end']}' )	
			";
 	  
	$result = my_query($query);
	$i = 0;
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		
		//if(! in_array( $karyawan['nik'] , $allow ) )continue;

		$content = get_html_content($karyawan['nik']); 
		$i++;
		//mailing_slip_absen($periode_absen , $karyawan['nama'] , $karyawan['nik'] , 'gunturris@gmail.com', $content);
		mailing_slip_absen($periode_absen , $karyawan['nama'] , $karyawan['nik'] , $karyawan['email_km'], $content);
	}
	return true;  
}


function page_kalkulasi(){
	my_set_code_js('
	$(document).ready(function() {
       	$(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&nik_start='.$_GET['nik_start'].'&nik_end='.$_GET['nik_end'].'&task=sendmail_proses&'.rand(0,9).'='.rand(0,99999).'\'); 
	});
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}
